<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Equipment Config</title>
	<link rel="stylesheet" href="../js/easyui/themes/bootstrap/easyui.css">
	<link rel="stylesheet" href="../js/easyui/themes/icon.css">
	<!--link rel="stylesheet" href="../css/index-ui.css" /-->
	<link rel="stylesheet" href="../css/eq-config.css" />
	<script type="text/javascript" src="../js/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/ams.util.js"></script>
	<script type="text/javascript" src="../js/webapi.js"></script>
	<script type="text/javascript" src="../js2/loginCookies.js"></script>
</head>

<body onload="on_page_load()">
<!--label id="test_label"> CONFIG </label-->

<table id="eq_cfg_grid" class="easyui-datagrid" title="" style="width:100%;height:90%"
	data-options="ctrlSelect:true,rownumbers:true,singleSelect:true,autoRowHeight:'true'">
<thead>
	<tr>
		<th data-options="field:'unit',width:140,align:'center',formatter:formatPrice">Module</th>
		<th data-options="field:'name',width:200,align:'center'	">Name</th>
		<th data-options="field:'val',width:150,align:'center'">Value</th>
		<th data-options="field:'desc',width:300">Description</th>
	</tr>
</thead>
</table>

<div id="set_value_dlg" class="easyui-dialog" title="Set Config Value" style="width:330px;height:250px;padding:10px;"
	data-options="modal:true,closed:true,iconCls:'icon-save',
		buttons: [
			{text:'OK', iconCls:'icon-ok',handler:function(){
				var selVal = $('#input_nv').textbox('getText') // document.getElementById('input_nv').value
				console.log(selVal)
				set_config_value(selVal)
				$('#set_value_dlg').dialog('close')
			}},
			{text:'Cancel', handler:function(){
				$('#set_value_dlg').dialog('close')
			}}
		]">
	<div>
		<table style="width:290px" class="cfg_set_val_dlg_table">
			<tr>
				<td colspan="2" >
					<label id="ec_name">{config-name}</label>
					<br/><br/>
				</td>
			</tr>
			<tr>
				<td width="145px"> Current Value: </td>
				<td>
					<input type="text" id="input_cv" class="easyui-textbox" value="{old value}" readonly="readonly" />
				</td>
			</tr>
			<tr height="4"></tr>
			<tr>
				<td> New Value: </td>
				<td>
					<input type="text" id="input_nv" class="easyui-textbox" value="" />
				</td>
			</tr>
			<tr height="50px">
				<td id="ec_desc" colspan="2" style="word-wrap:break-word;word-break:break-all">
					Desc: Blah...Blah...Blah...Blah...Blah...Blah...Blah...
				</td>
			</tr>
		</table>
	</div>

</div>
</body>

<script language="javascript">

function on_page_load()
{	
	$('#eq_cfg_grid').datagrid({
		onDblClickCell: function(index,field,value){
			if (field == 'val') {
				//on_edit_config_value()
			}
		}
	})

	if (ams_is_sim_mode()) {
		$.getJSON('config_01.json', 
			function(json_data) {
				$('#eq_cfg_grid').datagrid({
					data : json_data.rows
				})
			})
	} else {
		var eqn = get_page_param('eq')

		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getConfigValueAll"),	
			data:JSON.stringify({ 'eqName': eqn}),
			dataType: 'json',
			success : function(json_data) {
				console.log(JSON.parse(json_data.d))
				$(eq_cfg_grid).datagrid({
					data : JSON.parse(json_data.d).rows
				})
			},
			error: function (response) {
				//alert(errorThrown.message);
				//alert('Error: ' + response.responseText)
				$.messager.alert("Error", "Error2: " + response.responseText, "error");
			}
		});

		// $.post(getWebServiceUrl("getConfigValueAll"),
		// 	{ 'eqName': eqn },
		// 	function(json_data) {
		// 		$(eq_cfg_grid).datagrid({
		// 			data : json_data.rows
		// 		})
		// 	},
		// 	'json')
		// 	.fail(function(response) {
		// 		alert('Error: ' + response.responseText)
		// 	})
	}
}

function on_edit_config_value()
{
	var row = $('#eq_cfg_grid').datagrid('getSelected')
	if (!row) {
		return
	}
//	console.log(row)
	$('#set_value_dlg').dialog('open')
	document.getElementById('ec_name').innerHTML = row.name
	$('#input_cv').textbox('setText', row.val)
	$('#input_nv').textbox('setText', '')
	document.getElementById('ec_desc').innerHTML = row.desc
}

// 2019-06-26 11:11, jxb
function add_log_to_server(eqName, opType, opContent)
{
	var userLogin = GetCookie('ams_user_name');
	$.ajax({
		type: 'POST',
		contentType: 'application/json',
		url: getWebServiceUrl("setUserOpLogs"),
		data:JSON.stringify({
			"loginUser":userLogin,
			"eqName":eqName,
			"opType":opType,
			"opContent":opContent
		}),
		dataType: 'json',
		success: function (json_data) {
		},
		error: function (response) {
			$.messager.alert("Error", "Error: " + response.responseText, "error");
		}
	});
}

function set_config_value(new_val)
{
	if (ams_is_sim_mode()) {
	//	alert('set value: ' + new_val)
		var row = $('#eq_cfg_grid').datagrid('getSelected')
		row.val = new_val
		var _idx = $('#eq_cfg_grid').datagrid('getRowIndex', row)
		$('#eq_cfg_grid').datagrid('refreshRow', _idx)
	} else {
		if (checkUserLevel() == false){
			return;
		}
		var row = $('#eq_cfg_grid').datagrid('getSelected')
		var eqn = get_page_param('eq')
		var userLogin = GetCookie('ams_user_name');
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("setConfigValue"),
			data:JSON.stringify({
				'userLogin': userLogin,
				'eqName': eqn, 
				'ecName':row.name, 
				'ecValue':new_val
			}),
			dataType: 'json',
			success: function (json_data) {

				if(json_data.d == "0"){
					$.messager.alert("Tips", "Modified success.", "info");
					$.ajax({
						type: 'POST',
						contentType: 'application/json',
						url: getWebServiceUrl("getConfigValue"),
						data:JSON.stringify({
							'eqName':eqn, 
							'ecName':row.name 
						}),
						dataType: 'json',
						success: function (json_data) {
							var info = "Set config value(" + row.name + ") from \\'" + row.val + "\\' to \\'" + new_val + "\\'.";
							add_log_to_server(eqn, "config", info);

							row.val = new_val
							var _idx = $('#eq_cfg_grid').datagrid('getRowIndex', row)
							$('#eq_cfg_grid').datagrid('refreshRow', _idx)
						},
						error: function (response) {
							$.messager.alert("Error", "Error2: " + response.responseText, "error");	
							//alert('Error: ' + response.responseText)
						}
					});
				}else if(json_data.d == "-1"){
					$.messager.alert("Tips", "Modification failed.", "info");
				}
			},
			error: function (response) {
				$.messager.alert("Error", "Error2: " + response.responseText, "error");	
				//alert('Error: ' + response.responseText)
			}
		});
	}
}

function formatPrice(val,row){
	if (val.length > 0){
		return '<span style="color:blue;">' + val + '</span>';
	} else {
		return val;
	}
}
</script>

</html>
